// window.onload = getallscripts;  //get all script
window.onload = getdgroups;
function getallscripts() {
    const sctemplates = new XMLHttpRequest();
    sctemplates.open('GET', "http://127.0.0.1:5000/script", true);
    sctemplates.send(null);
    sctemplates.onreadystatechange = function () {
        if (sctemplates.readyState == 4) {
            let scsult = sctemplates.response;
            const sctemplatenames = document.getElementById('sctemplatename').value;
            scsult = JSON.parse(scsult);
            let opt = document.createElement('option');
            for (let i = 0; i < scsult.length; i++) {
                let scropt = scsult[i];
                opt.value = scsult[i].id;
                document.getElementById("sctemplatename").options.add(new Option(scsult[i].scriptname,scsult[i].id));
            }
        }
    }
}

// get all devicegroups
function getdgroups() {
    const getdgs = new XMLHttpRequest();
    getdgs.open('GET', "http://127.0.0.1:5000/system", true);
    getdgs.send(null);
    getdgs.onreadystatechange = function () {
        if (getdgs.readyState == 3) {
                const selectgroup = document.getElementById('selectgroup').value;
                let selectgroupdb = getdgs.response;
                    selectgroupdb = JSON.parse(selectgroupdb);
                    for (let i = 0; i < selectgroupdb.length; i++){
                        if (selectgroupdb[i].opttypes === "devicegroups"){
                            console.log(selectgroupdb[i]);
                            selectgroup.value = selectgroupdb[i].optvalues
                            document.getElementById("selectgroup").options.add(new Option(selectgroupdb[i].optdesc,selectgroupdb[i].optvalues));
                        }
                    }
        }
    }

}
//get  scritpt input to exterea
function getsc() {
    const scid = document.getElementById('sctemplatename').value;
    const sctemplates = new XMLHttpRequest();
    sctemplates.open('GET', 'http://127.0.0.1:5000/runscript?sctemplates=' + scid,);
    sctemplates.send();
    sctemplates.onreadystatechange = function () {
        if (sctemplates.readyState == 4) {
            let shell = sctemplates.response;
            shell = JSON.parse(shell);
            const shelltemp = (shell[0].scripts);
            const runsheltemp = document.getElementById('script');
            runsheltemp.innerHTML = shelltemp;
        }
    }
}
// pst sc to run
function run() {
    const shelldata = new FormData();
    const shell = document.getElementById('script').value;
    shelldata.append('shell', shell);
    const devicelists = document.getElementsByTagName('input');
    for (let i = 0; i < devicelists.length; i++) {
        if (devicelists[i].checked)
            shelldata.append('host', devicelists[i].dataset.ip);
            // console.log(shelldata);
             }
     const runshell = new XMLHttpRequest();
     const div9 = document.getElementById('shellresult');
     runshell.open('POST', 'http://localhost:5000/runscript');
     runshell.send(shelldata);
     runshell.onreadystatechange = function () {
            const devicetable1 = document.getElementById('devicetable1');
            if (runshell.readyState == 4 || runshell.status == 200) {
                let shellresult = runshell.response;
                 shellresult = JSON.parse(shellresult);
                console.log(shellresult);
                let html1 = '';
                for (let i = 0; i < shellresult.length; i++) {
                    let result1 = shellresult[i];
                    html1 += "<tr>";
                    html1 += "<td>" + i + "</td><td>" + result1.host + "</td><td>" + result1.code + "</td><td>" + result1.out + "</td>";
                    html1 += "</tr>";
                }
                devicetable1.innerHTML = html1;
                div9.style.display = 'inline';
            }
        }

}

//ѡ���������
function chosgroup() {
        const deivcediv = document.getElementById('dtable');
        const devicesgroups = document.getElementById('selectgroup').value;
        // console.log(devicesgroups);
        const getdevicegroup = new XMLHttpRequest();
        getdevicegroup.open('GET', 'http://127.0.0.1:5000/runscript?devicegroup=' + devicesgroups,);
        getdevicegroup.send();
        getdevicegroup.onreadystatechange = function () {
            const hostlits = document.getElementById("hostlits");
            if (getdevicegroup.readyState == 4) {
                let result = getdevicegroup.response;
                result = JSON.parse(result);
                let html = '';
                for (let i = 0; i < result.length; i++) {
                    let result1 = result[i];
                    html += "<tr>";
                    html += "<td><input type=\"checkbox\" data-ip=\"" + result1.ipaddr + "\"></td><td>" + result1.id + "</td><td>" + result1.ipaddr + "</td><td>" + result1.Desc + "</td>";
                    html += "</tr>";
                }
                hostlits.innerHTML = html;
                deivcediv.style.display='inline';
            }
        }
    }

    function gethostid() {
        const devicelists = document.getElementsByTagName('input');
        console.log(devicelists);
        for (let i = 0; i < devicelists.length; i++) {
            if (devicelists[i].checked)
                console.log(devicelists[i].dataset.ip)
        }
    }

